// script.js
// 添加键盘事件
document.addEventListener('keydown', (e) => {
    if(e.key === 'ArrowLeft') movePaddle('left');
    if(e.key === 'ArrowRight') movePaddle('right');
});

// 图片轮播功能
let currentSlide = 0;
const slides = document.querySelectorAll('.slides img');
const totalSlides = slides.length;

// 切换函数
function showSlide(n) {
    slides.forEach(slide => {
        slide.classList.remove('active');
        slide.style.zIndex = 0;
    });
    currentSlide = (n + totalSlides) % totalSlides;
    slides[currentSlide].classList.add('active');
    slides[currentSlide].style.zIndex = 1;
}

// 自动播放
let slideInterval = setInterval(() => showSlide(currentSlide + 1), 3000);

// 左右按钮事件
document.querySelector('.prev').addEventListener('click', () => {
    clearInterval(slideInterval);
    showSlide(currentSlide - 1);
    slideInterval = setInterval(() => showSlide(currentSlide + 1), 3000);
});

document.querySelector('.next').addEventListener('click', () => {
    clearInterval(slideInterval);
    showSlide(currentSlide + 1);
    slideInterval = setInterval(() => showSlide(currentSlide + 1), 3000);
});

// 音乐播放控制
const audio = document.getElementById('bgm');
function togglePlay() {
    audio.paused ? audio.play() : audio.pause();
    document.querySelector('.music-btn i').classList.toggle('fa-play');
    document.querySelector('.music-btn i').classList.toggle('fa-pause');
}

// 计算器功能
let display = document.getElementById('display');

function appendToDisplay(value) {
    display.value += value;
}

function clearDisplay() {
    display.value = '';
}

function calculate() {
    try {
        display.value = eval(display.value.replace('×', '*'));
    } catch (error) {
        display.value = '错误';
        setTimeout(clearDisplay, 1000);
    }
}

// 页面加载完成后初始化
window.onload = () => {
    showSlide(0);
    // 添加键盘事件支持
    document.addEventListener('keydown', (e) => {
        if (e.key >= '0' && e.key <= '9' || '+-*/.'.includes(e.key)) {
            appendToDisplay(e.key);
        } else if (e.key === 'Enter') {
            calculate();
        } else if (e.key === 'Escape') {
            clearDisplay();
        }
    });
}